import Vue 			from 'vue'
import util 		from '../../util'
import axios 		from 'axios'
import { Toast,Dialog,List } 	from 'vant';
Vue.use(Toast,Dialog,List);

export default {
	name: 'redExchangeHistory',
	data(){
		return {
			pageSize:1,
			finished:false,
			loadError:false,
			loading:false,
			offset:0,
			list:[]
		}
	},
	methods:{
		init(){
			let self = this;
			setTimeout(()=>{
				let params = {
					token:self.token || util.getToken(),
					page:self.pageSize
				}
				util.fetch2(`${util.ajaxUrl}/red-packet/exchange-list`,params).then(res => {
					if(res.retCode === 0){
						self.list = self.list.concat(res.retData.list);
						if(res.retData.list.length == 0) self.finished = true;
						self.pageSize++;
					}else{
						self.finished = true;
						Toast(res.retMsg || res.retHtml || '未知错误')
					}
					self.loading = false;
				}).catch(error => {
					console.log(error)
				})
			},500)
		},
		goBack(){
			util.navigateBack(-1,this)
		}
	},
	created() {
		util.waitSDKReady().then(token => {
			this.token = token;
		}).catch((e) => {
			Toast('获取token失败')
		})
		setTimeout(() => {
			util.registerHandler('refreshCommonRequestWhenGoBackView',res => {
				if(this.token) {
					this.finished = false;
					this.pageSize = 1;
					this.loading = false;
					this.init();
				}
				util.callHandler('setStatusBarStyle',{
					style:'dark'
				})
			})
			//退出登录的时候native调用H5方法清除token缓存
			util.registerHandler('loginOutDeleteToken',res => {
				window.token = '';
				this.token = '';
				localStorage.removeItem('token');
			})
			util.callHandler('setStatusBarStyle',{
				style:'dark'
			})
		},500)
    }
}